
package top.jamsee.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.jamsee.entity.Lent_data;
import top.jamsee.entity.RegisterHouse;
import top.jamsee.entity.ResultInfo;
import top.jamsee.service.LentService;
import top.jamsee.service.RegisterHouseService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/registerHouse")
//房屋买卖登记表
public class RegisterHouseController extends BaseController{



    int allPages=0; //所有页数


    //跳转到买房页面的接口
    @RequestMapping(value = "/buyBlank.do",method = RequestMethod.GET)
    public ModelAndView buy_blank(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("lent");
        return mv;
    }
    //跳转到租房页面的接口
    @RequestMapping(value = "/lentBlank.do")
    public ModelAndView lent_blank(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("forward:/WEB-INF/view/lent.html");
        return mv;
    }
    //跳转到买房租房登记页面的接口
    @RequestMapping(value = "/buyLentRegBlank.do")
    public ModelAndView buyLentReg_blank(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("buyAndLentRegister");
        return mv;
    }



    @Resource
    RegisterHouseService registerHouseService;
    /*
     * 查询房屋登记表的数据
     * @return :对应查询的数据
     *
     * */
    @RequestMapping(value = "/registerHousePage.do")
    @ResponseBody
    public List<RegisterHouse> registerHousePage(HttpServletRequest req, HttpServletResponse resp, int pageNum){
        //如果用户没有登录则不给他数据
        if (session.getAttribute("user")==null){
            return new ArrayList<RegisterHouse>();
        }
        //获取一个 ls对象,里面包括 数据信息和分页的总数
        List<List> ls=registerHouseService.SelectRegisterHouseData(pageNum);
        List<RegisterHouse> registerHouse_data=ls.get(0);
        List pageList=ls.get(1);
        //总页数
        allPages = (Integer) pageList.get(0);
        return registerHouse_data;




    }
    //返回总的页数
    @RequestMapping(value = "/allPage.do")
    @ResponseBody
    public int allPage(HttpServletRequest req,HttpServletResponse resp){
        return registerHouseService.selectRegisterHouseNum();
    }

    //根据编号删除数据
    @RequestMapping(value = "/registerHouseDel.do",method = RequestMethod.POST)
    @ResponseBody
    public int registerHouseDel(HttpServletRequest req,HttpServletResponse resp,int id){
        //如果用户没有登录则不给他数据
        if (session.getAttribute("user")==null) {
            //没有权限删除数据
            return 0;
        }
        int res = registerHouseService.deleteRegisterHouseData(id);
        return res;





    }
    //添加房屋登记表的记录
    @RequestMapping(value = "/addRegisterHouse.do")
    @ResponseBody
    public int addRegisterHouse(RegisterHouse registerHouse){
        System.out.println(registerHouse);
        if (session.getAttribute("user")==null) {
            //没有权限添加
            return 0;
        }
        //获取当前系统时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String nowTime=df.format(new Date());
        // new Date()为获取当前系统时间
        //String怎么转成Date
        int res=registerHouseService.addRegisterHouseData(registerHouse);

        return res;



    }
    /*
     * 模糊查询访问登记表
     * @part :传入部分的字符串
     * @pageNum: 传入的页数
     *
     *
     * */
    @RequestMapping(value = "/partRegisterHouse.do")
    @ResponseBody
    public List<RegisterHouse> partRegisterHouse(String part,int pageNum){
        //没有权限模糊查询
        if (session.getAttribute("user")==null) {
            return new ArrayList<RegisterHouse>();
        }

        //获取一个 ls对象,里面包括 数据信息和分页的总数
        List ls = registerHouseService.selectRegisterHouseByData(pageNum, part);
        List<RegisterHouse> register_data = (List<RegisterHouse>) ls.get(0);
        //总页数
        allPages = (Integer) ls.get(1);
        return register_data;

    }
    //返回总的页数(当模糊查询时)
    @RequestMapping(value = "/allPageSelectWhenSearch.do")
    @ResponseBody
    public int allPageSelectWhenSearch(String contact_name){

        return registerHouseService.selectRegisterHouseByDataNum(contact_name);
    }
    //返回总的页数(当查询时)
    @RequestMapping(value = "/allPageSelect.do")
    @ResponseBody
    public int allPageSelect(){
        return allPages;
    }
    //返回总的页数(当查询时)
    @RequestMapping(value = "/updateRegisterHouse.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultInfo updateRegisterHouse(RegisterHouse registerHouse_data){
        if (session.getAttribute("user")==null) {
            return new ResultInfo(false,"没有权限更新数据");
        }
        //搜索不到该记录
        if (registerHouseService.selectRegisterHouseById(registerHouse_data.getId())==0){
            return new ResultInfo(false,"搜索不到该记录");
        }
        if (registerHouse_data!=null){
            boolean upDateRes=registerHouseService.upDateRegisterHouseById(registerHouse_data);
            ResultInfo res=new ResultInfo(true);
            res.setMessage("更新成功");
            return res;
        }else {
            ResultInfo res=new ResultInfo(false);
            res.setMessage("编号不能为空");
            return res;
        }





    }





}
